#pragma once

#include "Business/Market.h"

class Leg
{
public:

	Leg(Market const& market, Direction::TYPE direction, Operation::TYPE operation) 
		:m_contract(market.GetContract()),m_direction(direction),m_operation(operation),m_market(market)
	{
	}

	double GetLimitPrice() const
	{
		if (IsBuy())
			return m_market.GetUpperLimit();
		else
			return m_market.GetLowerLimit();
	}

	int GetSpecialLimitPrice() const
	{
		if (IsBuy())
			return m_market.GetSpecialUpperLimit();
		else
			return m_market.GetSpecialLowerLimit();
	}

	double GetPrice() const
	{
		if (IsSell())
			return m_market.GetBuyPrices()[0].GetPrice();
		else
			return m_market.GetSellPrices()[0].GetPrice();
	}

	int GetQuantity() const
	{
		if (IsSell())
			return m_market.GetBuyPrices()[0].GetQuantity();
		else
			return m_market.GetSellPrices()[0].GetQuantity();
	}

	Direction::TYPE NegateDirection() const
	{
		return GetNegative(GetDirection());
	}

	double GetPreSettlementPrice() const
	{
		return m_market.GetPreSettlementPrice();
	}

	double GetMarginAmount(int quantity) const
	{
		return m_market.CalculateMargin(quantity, m_operation);
	}

	double GetCharge(int quantity) const
	{
		return m_market.CalculateCharge(GetPrice(),quantity,GetOperation());
	}

	double GetFreezeAmount(int quantity) const
	{
		return GetMarginAmount(quantity) + GetCharge(quantity);
	}

	Market const& GetMarket() const { return m_market; }

	int GetContract() const { return m_contract; }

	Direction::TYPE GetDirection() const { return m_direction; }
	Operation::TYPE GetOperation() const { return m_operation; }
	bool IsBuy() const { return m_direction == Direction::BUY; }
	bool IsSell() const { return m_direction == Direction::SELL; }
	bool IsOpen() const { return m_operation == Operation::OPEN; }

	bool Match(int contract, Direction::TYPE direction, Operation::TYPE operation) const
	{
		return m_contract == contract && m_direction == direction && m_operation == operation;
	}
private:
	int m_contract;
	Direction::TYPE m_direction;
	Operation::TYPE m_operation;
	Market const& m_market;
};

